export const setStorageSync = (key,value) => {
	try {
		uni.setStorageSync(key, value);
	} catch (e) {
		// error
	}
}

export const getStorageSync = (key) => {
	// return new Promise((resolve, reject) => {
		try {
			const value = uni.getStorageSync(key);
			return value || ''
		} catch (e) {
			// error
			reject('获取失败')
		}
	// })
	
}

export const removeStorageSync = (key) => {
	try {
		uni.removeStorageSync(key);
	} catch (e) {
		// error
	}
}

export const clearStorageSync = () => {
	try {
		uni.clearStorageSync();
	} catch (e) {
		// error
	}
}
export const callPhone = (phoneNumber) => {
      // 判断是否为字符串，并且是否为纯数字
      if (typeof phoneNumber === 'string' && /^\d+$/.test(phoneNumber)) {
        uni.makePhoneCall({
          phoneNumber: phoneNumber, // 电话号码
          success: function () {
            console.log('拨打电话成功');
          },
          fail: function () {
            console.log('拨打电话失败');
          }
        });
      } else {
        console.log('电话号码不正确');
		uni.showToast({
			icon:'none',
			title:'电话号码不正确'
		})
      }
    }

export const uploadFile = () => {
	return new Promise((resolve, reject) => {
		  const satoken  = getStorageSync('satoken') || '';
		uni.chooseImage({
			count: 1,
			mediaType: ['image'],
			sourceType: ['album', 'camera'],
			camera: 'back',
			success:(files) => {
				uni.showLoading({
					title:'上传中……',
					mask: true
				})
				uni.uploadFile({
					url: 'https://staff.myxiangyu.com/file/upload', 
					filePath: files.tempFiles[0].path,
					name: 'file',
					header:{satoken},
					success: (uploadFileRes) => {
						if(uploadFileRes.data){
							const data = JSON.parse(uploadFileRes.data)
							resolve(data.data)
						}
					},
					complete:() => {
						uni.hideLoading()
					}
				});
			},
			fail:(err) => {
				console.log('---err',err)
				// uni.showLoading({
				// 	title:'fail:---'+JSON.stringify(err),
				// 	mask: true,
				// 	duration:3000
				// })
			}
			})
	})
}
